﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SpriteEditor
{
    public class Module : SpriteCalculation
    {
        public int Index { get; set; }
        public int ID { get; set; }
        public String Module_Type { get; set; }
        public String Module_Color { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public int W { get; set; }
        public int H { get; set; }
        public string Desc { get; set; }

        public Bitmap img { get; set; }

        public Module clone()
        {
            Module m = new Module();

            m.ID = this.ID;
            m.Index = this.Index;
            m.Module_Type = this.Module_Type;
            m.Module_Color = this.Module_Color;
            m.X = this.X;
            m.Y = this.Y;
            m.W = this.W;
            m.H = this.H;
            m.Desc = this.Desc;

            if (this.img != null)
            {
                m.img = new Bitmap(this.img);
                m.img = (Bitmap)this.img.Clone();
            }

            return m;
        }

        public bool compareTo(Module m)
        {
            return (this.ID == m.ID && this.X == m.X && this.Y == m.Y && this.H == m.H && this.W == m.W);
        }

        public void GenerateImage(Bitmap full_bitmap)
        {
            if (full_bitmap != null)
            {
                Rectangle tmp_rect = new Rectangle(X, Y, W, H);
                try
                {
                    if (isImageType())
                    {
                        img = full_bitmap.Clone(tmp_rect, full_bitmap.PixelFormat);
                    }
                    else if (isRectType())
                    {
                        img = new Bitmap(W, H);
                        Graphics canvas = Graphics.FromImage(img);
                        Pen rect_pen = new Pen(Color.FromArgb(getColor()));

                        canvas.DrawRectangle(rect_pen, new Rectangle(0, 0, W - 1, H - 1));
                    }
                    else if (isFillRectType())
                    {
                        img = new Bitmap(W, H);
                        Graphics canvas = Graphics.FromImage(img);
                        SolidBrush fillrect_brush = new SolidBrush(Color.FromArgb(getColor()));

                        canvas.FillRectangle(fillrect_brush, new Rectangle(0, 0, W, H));
                    }
                }
                catch (OutOfMemoryException)
                {
                    //Outside image
                    img = null;
                }
                catch (ArgumentException)
                {
                    //Rect is zero
                    img = null;
                }
            }
        }

        public bool isImageType()
        {
            return (XmlEngine.getTypeOfModule_Int(Module_Type) == XmlEngine.TYPE_IMAGE);
        }

        public bool isNoneImageType()
        {
            return !(XmlEngine.getTypeOfModule_Int(Module_Type) == XmlEngine.TYPE_IMAGE);
        }

        public bool isRectType()
        {
            return (XmlEngine.getTypeOfModule_Int(Module_Type) == XmlEngine.TYPE_RECT);
        }

        public bool isFillRectType()
        {
            return (XmlEngine.getTypeOfModule_Int(Module_Type) == XmlEngine.TYPE_FILL_RECT);
        }

        public int getColor()
        {
            if (Module_Color == null || Module_Color == "")
                return -1;

            try
            {
                return Convert.ToInt32(Module_Color, 16);
            }
            catch (OverflowException)
            {
                //Outside the range of the Int32 type.
                return -1;
            }
            catch (FormatException)
            {
                // Not in a recognizable format.
                return -1;
            }
        }

        public int getColorA()
        {
            if (Module_Color == null || Module_Color == "")
                return -1;

            if (Module_Color.Length != 8)
                return -1;

            String colorA = Module_Color.Substring(0, 2);
            try
            {
                return Convert.ToInt32(colorA, 16);
            }
            catch (OverflowException)
            {
                //Outside the range of the Int32 type.
                return -1;
            }
            catch (FormatException)
            {
                // Not in a recognizable format.
                return -1;
            }
        }

        public int getColorR()
        {
            if (Module_Color == null || Module_Color == "")
                return -1;

            if (Module_Color.Length != 8)
                return -1;

            String colorR = Module_Color.Substring(2, 2);
            try
            {
                return Convert.ToInt32(colorR, 16);
            }
            catch (OverflowException)
            {
                //Outside the range of the Int32 type.
                return -1;
            }
            catch (FormatException)
            {
                // Not in a recognizable format.
                return -1;
            }
        }

        public int getColorG()
        {
            if (Module_Color == null || Module_Color == "")
                return -1;

            if (Module_Color.Length != 8)
                return -1;

            String colorG = Module_Color.Substring(4, 2);
            try
            {
                return Convert.ToInt32(colorG, 16);
            }
            catch (OverflowException)
            {
                //Outside the range of the Int32 type.
                return -1;
            }
            catch (FormatException)
            {
                // Not in a recognizable format.
                return -1;
            }
        }

        public int getColorB()
        {
            if (Module_Color == null || Module_Color == "")
                return -1;

            if (Module_Color.Length != 8)
                return -1;

            String colorB = Module_Color.Substring(6, 2);
            try
            {
                return Convert.ToInt32(colorB, 16);
            }
            catch (OverflowException)
            {
                //Outside the range of the Int32 type.
                return -1;
            }
            catch (FormatException)
            {
                // Not in a recognizable format.
                return -1;
            }
        }

        public void setColor(int color)
        {
            Module_Color = color.ToString("X8");
        }

        public int getType()
        {
            return (XmlEngine.getTypeOfModule_Int(Module_Type));
        }

        public void setType(int m_type)
        {
            Module_Type = XmlEngine.getTypeOfModule_String(m_type);
        }
    }
}
